﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bzzb.Model;
using Bzzb.Util;
using FreeSql.Internal.Model;
using Masuit.Tools.DateTimeExt;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Bzzb.Controllers
{
    public class GenerateDataController : BaseController
    {
        public static int i1 = -1;
        public static int i2 = -1;
        public static int i3 = -1;
        public static int i4 = -1;
        public static int L = 100;
        public static int KM = 0;
        public static int CI = 0;
        public static int YXYL = 100;
        public static int TOTALSYL = 0;
        public readonly ILogger<GenerateDataController> _logger;
        public GenerateDataController(ILogger<GenerateDataController> logger, IFreeSql fsql) : base(fsql)
        {
            this._logger = logger;
            GenerateDataController.i1++;
            if (GenerateDataController.i1 > JiShuQi.XIANLU1.Count -1) {
                GenerateDataController.i1 = -1;
            }
            GenerateDataController.i2++;
            if (GenerateDataController.i2 > JiShuQi.XIANLU2.Count -1)
            {
                GenerateDataController.i2 = -1;
            }
            GenerateDataController.i3++;
            if (GenerateDataController.i3 > JiShuQi.XIANLU3.Count -1)
            {
                GenerateDataController.i3 = -1;
            }
            GenerateDataController.i4++;
            if (GenerateDataController.i4 > JiShuQi.XIANLU4.Count -1)
            {
                GenerateDataController.i4 = -1;
            }
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Index()
        {
            // 1.保障车辆(1-电源车 2-加油车) 2.保障设备 3.保障构件车辆 4.保障构件设备
            if (GenerateDataController.YXYL < 0) {
                GenerateDataController.YXYL = 100;
            }
            if (GenerateDataController.L < 0)
            {
                GenerateDataController.L = 100;
            }

            #region 生成加油车传感数据
            // 加油车id列表
            JiaYouChe jiayouche1 = await _fsql.Select<JiaYouChe>().Where(jyc => jyc.Id == 1).ToOneAsync();

            BaoZhangZhuangBeiIOT jiayoucheiot1 = new();
            jiayoucheiot1.Bzzbtid = 1;
            jiayoucheiot1.Bzzbid = 2;
            jiayoucheiot1.Jtid = 1;
            jiayoucheiot1.Gjid = 0;
            jiayoucheiot1.Dpzt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 底盘状态
            jiayoucheiot1.Fdjzt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 发动机状态
            jiayoucheiot1.Xdcdy = new Random().Next(0, 200); // 蓄电池电压
            jiayoucheiot1.Yxyl = --GenerateDataController.YXYL + "-" + 100; // 邮箱油量
            jiayoucheiot1.Yh = new Random().Next(8, 18); // 油耗
            jiayoucheiot1.Ltqy = "2.5"; // 轮胎气压
            jiayoucheiot1.Sxwd = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 水箱温度
            jiayoucheiot1.Hy = new Random().Next(0, 1) == 1 ? "正常" : "异常"; // 滑油
            jiayoucheiot1.Xssd = new Random().Next(10, 60).ToString(); // 行驶速度
            jiayoucheiot1.Sczt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 刹车状态
            jiayoucheiot1.Zxwz = JiShuQi.XIANLU1[GenerateDataController.i1]; // 最新位置
            jiayoucheiot1.Ygyl = new Random().Next(1, 100) + "-" + 100; // 油罐油量
            jiayoucheiot1.Mqzt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 目前状态
            jiayoucheiot1.Ljsysj = ""; // 累计使用时间
            jiayoucheiot1.Lhdw = ""; // 累计使用量
            jiayoucheiot1.Ldw = ""; // 累计使用量
            jiayoucheiot1.Kmdw = ""; // 累计使用量
            jiayoucheiot1.Cidw = ""; // 累计使用量
            jiayoucheiot1.Tiandw = ""; // 累计使用量
            jiayoucheiot1.Hdw = ""; // 累计使用量
            jiayoucheiot1.Totalsj = Math.Round((DateTime.Now.GetTotalSeconds() - jiayouche1.Created.GetTotalSeconds()) / 60, 2).ToString();
            jiayoucheiot1.Totalsyl = (GenerateDataController.TOTALSYL++).ToString();
            // 计算使用量
            List<DingQiKongZhiXiang> jyc1dingqikongzhixiangs = await _fsql.Select<DingQiKongZhiXiang>().Where(dqkzx => dqkzx.Bzzbtid == 1 && dqkzx.Bzzbid == 2 && dqkzx.Jtid == 1 && dqkzx.Gjid == 0).ToListAsync();
            foreach (var dingqikongzhixiang in jyc1dingqikongzhixiangs)
            {
                jiayoucheiot1.Ljsysj = Math.Round((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / 60, 2).ToString(); // 累计使用时间，默认分钟

                JiShiXiang jishixiang = await _fsql.Select<JiShiXiang>().Where(jsx => jsx.Id == dingqikongzhixiang.Jsxid).ToOneAsync();
                //Console.WriteLine("加油车1计时项单位：" + jishixiang.Jldw.ToLower());
                if (jishixiang.Jldw.ToLower().Equals("l/h"))
                {
                    jiayoucheiot1.Lhdw = jiayoucheiot1.Yh.ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("l"))
                {
                    jiayoucheiot1.Ldw = (--GenerateDataController.L).ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("km"))
                {
                    jiayoucheiot1.Kmdw = (++GenerateDataController.KM).ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("次"))
                {
                    jiayoucheiot1.Cidw = (++GenerateDataController.CI).ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("天"))
                {
                    jiayoucheiot1.Tiandw = Math.Round((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / (24 * 60 * 60), 2).ToString(); // 累计使用时间，默认分钟
                }
                else if (jishixiang.Jldw.ToLower().Equals("h"))
                {
                    jiayoucheiot1.Hdw = Math.Round((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / (60 * 60), 2).ToString(); // 累计使用时间，默认分钟
                }
                else { }
            }
            await _fsql.Insert(jiayoucheiot1).ExecuteAffrowsAsync();

            JiaYouChe jiayouche2 = await _fsql.Select<JiaYouChe>().Where(jyc => jyc.Id == 2).ToOneAsync();

            BaoZhangZhuangBeiIOT jiayoucheiot2 = new();
            jiayoucheiot2.Bzzbtid = 1;
            jiayoucheiot2.Bzzbid = 2;
            jiayoucheiot2.Jtid = 2;
            jiayoucheiot2.Gjid = 0;
            jiayoucheiot2.Dpzt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 底盘状态
            jiayoucheiot2.Fdjzt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 发动机状态
            jiayoucheiot2.Xdcdy = new Random().Next(0, 200); // 蓄电池电压
            jiayoucheiot2.Yxyl = --GenerateDataController.YXYL + "-" + 100; // 邮箱油量
            jiayoucheiot2.Yh = new Random().Next(8, 18); // 油耗
            jiayoucheiot2.Ltqy = "2.5"; // 轮胎气压
            jiayoucheiot2.Sxwd = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 水箱温度
            jiayoucheiot2.Hy = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 滑油
            jiayoucheiot2.Xssd = new Random().Next(10, 60).ToString(); // 行驶速度
            jiayoucheiot2.Sczt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 刹车状态
            jiayoucheiot2.Zxwz = JiShuQi.XIANLU2[GenerateDataController.i2]; // 最新位置
            jiayoucheiot2.Ygyl = new Random().Next(1, 100) + "-" + 100; // 油罐油量
            jiayoucheiot2.Mqzt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 目前状态
            jiayoucheiot2.Ljsysj = ""; // 累计使用时间
            jiayoucheiot2.Lhdw = ""; // 累计使用量
            jiayoucheiot2.Ldw = ""; // 累计使用量
            jiayoucheiot2.Kmdw = ""; // 累计使用量
            jiayoucheiot2.Cidw = ""; // 累计使用量
            jiayoucheiot2.Tiandw = ""; // 累计使用量
            jiayoucheiot2.Hdw = ""; // 累计使用量
            jiayoucheiot2.Totalsj = Math.Round((DateTime.Now.GetTotalSeconds() - jiayouche2.Created.GetTotalSeconds()) / 60, 2).ToString();
            jiayoucheiot2.Totalsyl = (GenerateDataController.TOTALSYL++).ToString();
            // 计算使用量
            List<DingQiKongZhiXiang> jyc2dingqikongzhixiangs = await _fsql.Select<DingQiKongZhiXiang>().Where(dqkzx => dqkzx.Bzzbtid == 1 && dqkzx.Bzzbid == 2 && dqkzx.Jtid == 2 && dqkzx.Gjid == 0).ToListAsync();
            foreach (var dingqikongzhixiang in jyc2dingqikongzhixiangs)
            {
                jiayoucheiot2.Ljsysj = Math.Round((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / 60, 2).ToString(); // 累计使用时间，默认分钟

                JiShiXiang jishixiang = await _fsql.Select<JiShiXiang>().Where(jsx => jsx.Id == dingqikongzhixiang.Jsxid).ToOneAsync();
                //Console.WriteLine("加油车2计时项单位：" + jishixiang.Jldw.ToLower());
                if (jishixiang.Jldw.ToLower().Equals("l/h"))
                {
                    jiayoucheiot2.Lhdw = jiayoucheiot2.Yh.ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("l"))
                {
                    jiayoucheiot2.Ldw = (--GenerateDataController.L).ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("km"))
                {
                    jiayoucheiot2.Kmdw = (++GenerateDataController.KM).ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("次"))
                {
                    jiayoucheiot2.Cidw = (++GenerateDataController.CI).ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("天"))
                {
                    jiayoucheiot2.Tiandw = Math.Round((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / (24 * 60 * 60), 2).ToString(); // 累计使用时间，默认分钟
                }
                else if (jishixiang.Jldw.ToLower().Equals("h"))
                {
                    jiayoucheiot2.Hdw = Math.Round((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / (60 * 60), 2).ToString(); // 累计使用时间，默认分钟
                }
                else { }
            }
            await _fsql.Insert(jiayoucheiot2).ExecuteAffrowsAsync();
            #endregion

            #region 生成电源车传感数据
            // 电源车id列表
            DianYuanChe dianyuanche1 = await _fsql.Select<DianYuanChe>().Where(dyc => dyc.Id == 1).ToOneAsync();

            BaoZhangZhuangBeiIOT dianyuancheiot1 = new();
            dianyuancheiot1.Bzzbtid = 1;
            dianyuancheiot1.Bzzbid = 1;
            dianyuancheiot1.Jtid = 1;
            dianyuancheiot1.Gjid = 0;
            dianyuancheiot1.Dpzt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 底盘状态
            dianyuancheiot1.Fdjzt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 发动机状态
            dianyuancheiot1.Xdcdy = new Random().Next(0, 200); // 蓄电池电压
            dianyuancheiot1.Yxyl = --GenerateDataController.YXYL + "-" + 100; // 邮箱油量
            dianyuancheiot1.Yh = new Random().Next(8, 18); // 油耗
            dianyuancheiot1.Ltqy = "2.5"; // 轮胎气压
            dianyuancheiot1.Sxwd = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 水箱温度
            dianyuancheiot1.Hy = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 滑油
            dianyuancheiot1.Xssd = new Random().Next(10, 60).ToString(); // 行驶速度
            dianyuancheiot1.Sczt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 刹车状态
            dianyuancheiot1.Zxwz = JiShuQi.XIANLU3[GenerateDataController.i3]; // 最新位置
            dianyuancheiot1.Ygyl = new Random().Next(1, 100) + "-" + 100; // 油罐油量
            dianyuancheiot1.Mqzt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 目前状态
            dianyuancheiot1.Ljsysj = ""; // 累计使用时间
            dianyuancheiot1.Lhdw = ""; // 累计使用量
            dianyuancheiot1.Ldw = ""; // 累计使用量
            dianyuancheiot1.Kmdw = ""; // 累计使用量
            dianyuancheiot1.Cidw = ""; // 累计使用量
            dianyuancheiot1.Tiandw = ""; // 累计使用量
            dianyuancheiot1.Hdw = ""; // 累计使用量
            dianyuancheiot1.Totalsj = Math.Round((DateTime.Now.GetTotalSeconds() - dianyuanche1.Created.GetTotalSeconds()) / 60, 2).ToString();
            dianyuancheiot1.Totalsyl = (GenerateDataController.TOTALSYL++).ToString();
            // 计算使用量
            List<DingQiKongZhiXiang> dyc1dingqikongzhixiangs = await _fsql.Select<DingQiKongZhiXiang>().Where(dqkzx => dqkzx.Bzzbtid == 1 && dqkzx.Bzzbid == 1 && dqkzx.Jtid == dianyuancheiot1.Jtid && dqkzx.Gjid == 0).ToListAsync();
            foreach (var dingqikongzhixiang in dyc1dingqikongzhixiangs)
            {
                dianyuancheiot1.Ljsysj = Math.Round((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / 60, 2).ToString(); // 累计使用时间，默认分钟

                JiShiXiang jishixiang = await _fsql.Select<JiShiXiang>().Where(jsx => jsx.Id == dingqikongzhixiang.Jsxid).ToOneAsync();
                //Console.WriteLine("电源车1计时项单位：" + jishixiang.Jldw.ToLower());
                if (jishixiang.Jldw.ToLower().Equals("l/h"))
                {
                    dianyuancheiot1.Lhdw = dianyuancheiot1.Yh.ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("l"))
                {
                    dianyuancheiot1.Ldw = (--GenerateDataController.L).ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("km"))
                {
                    dianyuancheiot1.Kmdw = (++GenerateDataController.KM).ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("次"))
                {
                    dianyuancheiot1.Cidw = (++GenerateDataController.CI).ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("天"))
                {
                    dianyuancheiot1.Tiandw = Math.Round((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / (24 * 60 * 60), 2).ToString(); // 累计使用时间，默认分钟
                }
                else if (jishixiang.Jldw.ToLower().Equals("h"))
                {
                    dianyuancheiot1.Hdw = Math.Round((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / (60 * 60), 2).ToString(); // 累计使用时间，默认分钟
                }
                else { }
            }
            await _fsql.Insert(dianyuancheiot1).ExecuteAffrowsAsync();

            DianYuanChe dianyuanche3 = await _fsql.Select<DianYuanChe>().Where(dyc => dyc.Id == 3).ToOneAsync();

            BaoZhangZhuangBeiIOT dianyuancheiot3 = new();
            dianyuancheiot3.Bzzbtid = 1;
            dianyuancheiot3.Bzzbid = 1;
            dianyuancheiot3.Jtid = 3;
            dianyuancheiot3.Gjid = 0;
            dianyuancheiot3.Dpzt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 底盘状态
            dianyuancheiot3.Fdjzt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 发动机状态
            dianyuancheiot3.Xdcdy = new Random().Next(0, 200); // 蓄电池电压
            dianyuancheiot3.Yxyl = --GenerateDataController.YXYL + "-" + 100; // 邮箱油量
            dianyuancheiot3.Yh = new Random().Next(8, 18); // 油耗
            dianyuancheiot3.Ltqy = "2.5"; // 轮胎气压
            dianyuancheiot3.Sxwd = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 水箱温度
            dianyuancheiot3.Hy = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 滑油
            dianyuancheiot3.Xssd = new Random().Next(10, 60).ToString(); // 行驶速度
            dianyuancheiot3.Sczt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 刹车状态
            dianyuancheiot3.Zxwz = JiShuQi.XIANLU3[GenerateDataController.i4]; // 最新位置
            dianyuancheiot3.Ygyl = new Random().Next(1, 100) + "-" + 100; // 油罐油量
            dianyuancheiot3.Mqzt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 目前状态
            dianyuancheiot3.Ljsysj = ""; // 累计使用时间
            dianyuancheiot3.Lhdw = ""; // 累计使用量
            dianyuancheiot3.Ldw = ""; // 累计使用量
            dianyuancheiot3.Kmdw = ""; // 累计使用量
            dianyuancheiot3.Cidw = ""; // 累计使用量
            dianyuancheiot3.Tiandw = ""; // 累计使用量
            dianyuancheiot3.Hdw = ""; // 累计使用量
            dianyuancheiot3.Totalsj = Math.Round((DateTime.Now.GetTotalSeconds() - dianyuanche3.Created.GetTotalSeconds()) / 60, 2).ToString();
            dianyuancheiot3.Totalsyl = (GenerateDataController.TOTALSYL++).ToString();
            // 计算使用量
            List<DingQiKongZhiXiang> dyc3dingqikongzhixiangs = await _fsql.Select<DingQiKongZhiXiang>().Where(dqkzx => dqkzx.Bzzbtid == 1 && dqkzx.Bzzbid == 1 && dqkzx.Jtid == dianyuancheiot3.Jtid && dqkzx.Gjid == 0).ToListAsync();
            foreach (var dingqikongzhixiang in dyc3dingqikongzhixiangs)
            {
                dianyuancheiot3.Ljsysj = Math.Round((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / 60, 2).ToString(); // 累计使用时间，默认分钟

                JiShiXiang jishixiang = await _fsql.Select<JiShiXiang>().Where(jsx => jsx.Id == dingqikongzhixiang.Jsxid).ToOneAsync();
                //Console.WriteLine("电源车3计时项单位：" + jishixiang.Jldw.ToLower());
                if (jishixiang.Jldw.ToLower().Equals("l/h"))
                {
                    dianyuancheiot3.Lhdw = dianyuancheiot3.Yh.ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("l"))
                {
                    dianyuancheiot3.Ldw = (--GenerateDataController.L).ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("km"))
                {
                    dianyuancheiot3.Kmdw = (++GenerateDataController.KM).ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("次"))
                {
                    dianyuancheiot3.Cidw = (++GenerateDataController.CI).ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("天"))
                {
                    dianyuancheiot3.Tiandw = Math.Round((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / (24 * 60 * 60), 2).ToString(); // 累计使用时间，默认分钟
                }
                else if (jishixiang.Jldw.ToLower().Equals("h"))
                {
                    dianyuancheiot3.Hdw = Math.Round((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / (60 * 60), 2).ToString(); // 累计使用时间，默认分钟
                }
                else { }
            }
            await _fsql.Insert(dianyuancheiot3).ExecuteAffrowsAsync();
            #endregion

            #region 生成保障设备传感数据
            // 保障设备id列表
            List<long> bzsbids = await _fsql.Select<BaoZhangSheBei>().ToListAsync(bzsb => bzsb.Id);
            int i1 = new Random().Next(0, bzsbids.Count);
            BaoZhangZhuangBeiIOT baozhangshebeiiot = new();
            baozhangshebeiiot.Bzzbtid = 2;
            baozhangshebeiiot.Bzzbid = new Random().Next(1, 2);
            baozhangshebeiiot.Jtid = bzsbids[i1];
            baozhangshebeiiot.Gjid = 0;
            baozhangshebeiiot.Dpzt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 底盘状态
            baozhangshebeiiot.Fdjzt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 发动机状态
            baozhangshebeiiot.Xdcdy = new Random().Next(0, 200); // 蓄电池电压
            baozhangshebeiiot.Yxyl = --GenerateDataController.YXYL + "-" + 100; // 邮箱油量
            baozhangshebeiiot.Yh = new Random().Next(8, 18); // 油耗
            baozhangshebeiiot.Ltqy = "2.5"; // 轮胎气压
            baozhangshebeiiot.Sxwd = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 水箱温度
            baozhangshebeiiot.Hy = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 滑油
            baozhangshebeiiot.Xssd = new Random().Next(10, 60).ToString(); // 行驶速度
            baozhangshebeiiot.Sczt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 刹车状态
            baozhangshebeiiot.Zxwz = JiShuQi.WZ[new Random().Next(0, 9)]; // 最新位置
            baozhangshebeiiot.Ygyl = new Random().Next(1, 100) + "-" + 100; // 油罐油量
            baozhangshebeiiot.Mqzt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 目前状态
            baozhangshebeiiot.Ljsysj = ""; // 累计使用时间
            baozhangshebeiiot.Lhdw = ""; // 累计使用量
            baozhangshebeiiot.Ldw = ""; // 累计使用量
            baozhangshebeiiot.Kmdw = ""; // 累计使用量
            baozhangshebeiiot.Cidw = ""; // 累计使用量
            baozhangshebeiiot.Tiandw = ""; // 累计使用量
            baozhangshebeiiot.Hdw = ""; // 累计使用量
            BaoZhangSheBei baozhangshebei = await _fsql.Select<BaoZhangSheBei>().Where(bzsb => bzsb.Id == baozhangshebeiiot.Jtid).ToOneAsync();
            baozhangshebeiiot.Totalsj = Math.Round((DateTime.Now.GetTotalSeconds() - baozhangshebei.Created.GetTotalSeconds()) / 60, 2).ToString();
            baozhangshebeiiot.Totalsyl = (GenerateDataController.TOTALSYL++).ToString();
            // 计算使用量
            List<DingQiKongZhiXiang> bzsbdingqikongzhixiangs = await _fsql.Select<DingQiKongZhiXiang>().Where(dqkzx => dqkzx.Bzzbtid == 2 && dqkzx.Bzzbid == baozhangshebeiiot.Bzzbid && dqkzx.Jtid == baozhangshebeiiot.Jtid && dqkzx.Gjid == 0).ToListAsync();
            foreach (var dingqikongzhixiang in bzsbdingqikongzhixiangs)
            {
                baozhangshebeiiot.Ljsysj = Math.Round((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / 60, 2).ToString(); // 累计使用时间，默认分钟

                JiShiXiang jishixiang = await _fsql.Select<JiShiXiang>().Where(jsx => jsx.Id == dingqikongzhixiang.Jsxid).ToOneAsync();
                if (jishixiang.Jldw.ToLower().Equals("l/h"))
                {
                    baozhangshebeiiot.Lhdw = baozhangshebeiiot.Yh.ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("l"))
                {
                    baozhangshebeiiot.Ldw = (--GenerateDataController.L).ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("km"))
                {
                    baozhangshebeiiot.Kmdw = (++GenerateDataController.KM).ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("次"))
                {
                    baozhangshebeiiot.Cidw = (++GenerateDataController.CI).ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("天"))
                {
                    baozhangshebeiiot.Tiandw = Math.Round((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / (24 * 60 * 60), 2).ToString(); // 累计使用时间，默认分钟
                }
                else if (jishixiang.Jldw.ToLower().Equals("h"))
                {
                    baozhangshebeiiot.Hdw = Math.Round((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / (60 * 60), 2).ToString(); // 累计使用时间，默认分钟
                }
                else { }
            }
            await _fsql.Insert(baozhangshebeiiot).ExecuteAffrowsAsync();
            #endregion

            #region 生成保障工具数据
            // 加油车id列表
            List<long> bzgjids = await _fsql.Select<BaoZhangGongJu>().ToListAsync(bzgj => bzgj.Id);
            int i2 = new Random().Next(0, bzgjids.Count);
            //Console.WriteLine($"{bzgjids.Count}保障工具索引：" + i2 + "值：" + bzgjids[i2]);
            BaoZhangZhuangBeiIOT baozhanggongjuiot = new();
            baozhanggongjuiot.Bzzbtid = 5;
            baozhanggongjuiot.Bzzbid = 0;
            baozhanggongjuiot.Jtid = bzgjids[i2];
            baozhanggongjuiot.Gjid = 0;
            baozhanggongjuiot.Dpzt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 底盘状态
            baozhanggongjuiot.Fdjzt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 发动机状态
            baozhanggongjuiot.Xdcdy = new Random().Next(0, 200); // 蓄电池电压
            baozhanggongjuiot.Yxyl = --GenerateDataController.YXYL + "-" + 100; // 邮箱油量
            baozhanggongjuiot.Yh = new Random().Next(8, 18); // 油耗
            baozhanggongjuiot.Ltqy = "2.5"; // 轮胎气压
            baozhanggongjuiot.Sxwd = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 水箱温度
            baozhanggongjuiot.Hy = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 滑油
            baozhanggongjuiot.Xssd = new Random().Next(10, 60).ToString(); // 行驶速度
            baozhanggongjuiot.Sczt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 刹车状态
            baozhanggongjuiot.Zxwz = JiShuQi.WZ[new Random().Next(0, 9)]; // 最新位置
            baozhanggongjuiot.Ygyl = new Random().Next(1, 100) + "-" + 100; // 油罐油量
            baozhanggongjuiot.Mqzt = new Random().Next(0, 2) == 1 ? "正常" : "异常"; // 目前状态
            baozhanggongjuiot.Ljsysj = ""; // 累计使用时间
            baozhanggongjuiot.Lhdw = ""; // 累计使用量
            baozhanggongjuiot.Ldw = ""; // 累计使用量
            baozhanggongjuiot.Kmdw = ""; // 累计使用量
            baozhanggongjuiot.Cidw = ""; // 累计使用量
            baozhanggongjuiot.Tiandw = ""; // 累计使用量
            baozhanggongjuiot.Hdw = ""; // 累计使用量
            BaoZhangGongJu baozhanggongju = await _fsql.Select<BaoZhangGongJu>().Where(bzgj => bzgj.Id == baozhanggongjuiot.Jtid).ToOneAsync();
            baozhanggongjuiot.Totalsj = Math.Round((DateTime.Now.GetTotalSeconds() - baozhanggongju.Created.GetTotalSeconds()) / 60, 2).ToString();
            baozhanggongjuiot.Totalsyl = (GenerateDataController.TOTALSYL++).ToString();
            // 计算使用量
            List<DingQiKongZhiXiang> bzgjdingqikongzhixiangs = await _fsql.Select<DingQiKongZhiXiang>().Where(dqkzx => dqkzx.Bzzbtid == 1 && dqkzx.Bzzbid == 2 && dqkzx.Jtid == baozhanggongjuiot.Jtid && dqkzx.Gjid == 0).ToListAsync();
            foreach (var dingqikongzhixiang in bzgjdingqikongzhixiangs)
            {
                baozhanggongjuiot.Ljsysj = Math.Round((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / 60, 2).ToString(); // 累计使用时间，默认分钟

                JiShiXiang jishixiang = await _fsql.Select<JiShiXiang>().Where(jsx => jsx.Id == dingqikongzhixiang.Jsxid).ToOneAsync();
                //Console.WriteLine("计时项单位：" + jishixiang.Jldw.ToLower() + " 比较结果：" + jishixiang.Jldw.ToLower().Equals("l/h"));
                if (jishixiang.Jldw.ToLower().Equals("l/h"))
                {
                    baozhanggongjuiot.Lhdw = baozhanggongjuiot.Yh.ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("l"))
                {
                    baozhanggongjuiot.Ldw = (--GenerateDataController.L).ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("km"))
                {
                    baozhanggongjuiot.Kmdw = (++GenerateDataController.KM).ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("次"))
                {
                    baozhanggongjuiot.Cidw = (++GenerateDataController.CI).ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("天"))
                {
                    baozhanggongjuiot.Tiandw = Math.Round((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / (24 * 60 * 60), 2).ToString(); // 累计使用时间，默认分钟
                }
                else if (jishixiang.Jldw.ToLower().Equals("h"))
                {
                    baozhanggongjuiot.Hdw = Math.Round((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / (60 * 60), 2).ToString(); // 累计使用时间，默认分钟
                }
                else { }
            }
            await _fsql.Insert(baozhanggongjuiot).ExecuteAffrowsAsync();
            #endregion

            #region 生成装备构件传感数据
            // 装备构件id列表
            List<long> gjzbids = await _fsql.Select<GouJianZhuangBei>().ToListAsync(gjzb => gjzb.Id);

            BaoZhangZhuangBeiIOT gjbaozhangshebeiiot = new();
            if (new Random().Next(1, 3) == 1)
            {
                gjbaozhangshebeiiot.Bzzbtid = 3;
                gjbaozhangshebeiiot.Bzzbid = 1;
                gjbaozhangshebeiiot.Jtid = 1;
                gjbaozhangshebeiiot.Gjid = 1;
            }
            else {
                gjbaozhangshebeiiot.Bzzbtid = 4;
                gjbaozhangshebeiiot.Bzzbid = 1;
                gjbaozhangshebeiiot.Jtid = 1;
                gjbaozhangshebeiiot.Gjid = 2;
            }
            
            gjbaozhangshebeiiot.Dpzt = new Random().Next(0, 1) == 1 ? "正常" : "异常"; // 底盘状态
            gjbaozhangshebeiiot.Fdjzt = new Random().Next(0, 1) == 1 ? "正常" : "异常"; // 发动机状态
            gjbaozhangshebeiiot.Xdcdy = new Random().Next(0, 200); // 蓄电池电压
            gjbaozhangshebeiiot.Yxyl = --GenerateDataController.YXYL + "-" + 100; // 邮箱油量
            gjbaozhangshebeiiot.Yh = new Random().Next(8, 18); // 油耗
            gjbaozhangshebeiiot.Ltqy = "2.5"; // 轮胎气压
            gjbaozhangshebeiiot.Sxwd = new Random().Next(0, 1) == 1 ? "正常" : "异常"; // 水箱温度
            gjbaozhangshebeiiot.Hy = new Random().Next(0, 1) == 1 ? "正常" : "异常"; // 滑油
            gjbaozhangshebeiiot.Xssd = new Random().Next(10, 60).ToString(); // 行驶速度
            gjbaozhangshebeiiot.Sczt = new Random().Next(0, 1) == 1 ? "正常" : "异常"; // 刹车状态
            gjbaozhangshebeiiot.Zxwz = "最新位置（x, y）"; // 最新位置
            gjbaozhangshebeiiot.Ygyl = new Random().Next(1, 100) + "-" + 100; // 油罐油量
            gjbaozhangshebeiiot.Mqzt = new Random().Next(0, 1) == 1 ? "正常" : "异常"; // 目前状态
            gjbaozhangshebeiiot.Ljsysj = ""; // 累计使用时间
            gjbaozhangshebeiiot.Lhdw = ""; // 累计使用量
            gjbaozhangshebeiiot.Ldw = ""; // 累计使用量
            gjbaozhangshebeiiot.Kmdw = ""; // 累计使用量
            gjbaozhangshebeiiot.Cidw = ""; // 累计使用量
            gjbaozhangshebeiiot.Tiandw = ""; // 累计使用量
            gjbaozhangshebeiiot.Hdw = ""; // 累计使用量
            GouJianZhuangBei goujianzhuangbei = await _fsql.Select<GouJianZhuangBei>().Where(gjzb => gjzb.Id == gjbaozhangshebeiiot.Jtid).ToOneAsync();
            gjbaozhangshebeiiot.Totalsj = Math.Round((DateTime.Now.GetTotalSeconds() - goujianzhuangbei.Created.GetTotalSeconds()) / 60, 2).ToString();
            gjbaozhangshebeiiot.Totalsyl = (GenerateDataController.TOTALSYL++).ToString();
            // 计算使用量
            List<DingQiKongZhiXiang> gjdingqikongzhixiangs = await _fsql.Select<DingQiKongZhiXiang>().Where(dqkzx => dqkzx.Bzzbtid == gjbaozhangshebeiiot.Bzzbtid && dqkzx.Bzzbid == gjbaozhangshebeiiot.Bzzbid && dqkzx.Jtid == gjbaozhangshebeiiot.Jtid && dqkzx.Gjid == gjbaozhangshebeiiot.Gjid).ToListAsync();
            foreach (var dingqikongzhixiang in gjdingqikongzhixiangs)
            {
                gjbaozhangshebeiiot.Ljsysj = Math.Round((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / 60, 2).ToString(); // 累计使用时间，默认分钟

                JiShiXiang jishixiang = await _fsql.Select<JiShiXiang>().Where(jsx => jsx.Id == dingqikongzhixiang.Jsxid).ToOneAsync();
                if (jishixiang.Jldw.ToLower().Equals("l/h"))
                {
                    gjbaozhangshebeiiot.Lhdw = gjbaozhangshebeiiot.Yh.ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("l"))
                {
                    gjbaozhangshebeiiot.Ldw = (--GenerateDataController.L).ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("km"))
                {
                    gjbaozhangshebeiiot.Kmdw = (++GenerateDataController.KM).ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("次"))
                {
                    gjbaozhangshebeiiot.Cidw = (++GenerateDataController.CI).ToString(); // 累计使用量
                }
                else if (jishixiang.Jldw.ToLower().Equals("天"))
                {
                    gjbaozhangshebeiiot.Tiandw = Math.Round((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / (24 * 60 * 60), 2).ToString(); // 累计使用时间，默认分钟
                }
                else if (jishixiang.Jldw.ToLower().Equals("h"))
                {
                    gjbaozhangshebeiiot.Hdw = Math.Round((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / (60 * 60), 2).ToString(); // 累计使用时间，默认分钟
                    //Console.WriteLine("生成数据时间：" + DateTime.Now.ToString() + "qssj:" + dingqikongzhixiang.Qssj.ToString());
                    //Console.WriteLine("生成数据时间：" + DateTime.Now.GetTotalSeconds() + "qssj:" + dingqikongzhixiang.Qssj.GetTotalSeconds());
                }
                else { }
            }
            await _fsql.Insert(gjbaozhangshebeiiot).ExecuteAffrowsAsync();
            #endregion

            return RenderJson(data: "生成成功");
        }
    }
}
